package com.usooft.metaTalent.core.platform.query;

import com.usooft.framework.core.query.BaseSorts;
import com.usooft.framework.core.query.BaseWraps;
import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Sort;
import com.usooft.framework.core.query.Wrap;
import java.lang.Boolean;
import java.lang.IllegalArgumentException;
import java.lang.Long;
import java.lang.String;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class ImageQuery {
    private final Filter filter;

    private final Sorts sorts;

    private final Wraps wraps;

    private final Limit limit;

    @Getter
    public static class Filter {
        private final Long idIs;

        private final Long[] idIn;

        private final LocalDateTime createdDateOnOrBefore;

        private final LocalDateTime createdDateOnOrAfter;

        private final String nameContains;

        private final Boolean isDeleteIs;

        private Filter(Builder builder) {
            this.idIs = builder.idIs;
            this.idIn = builder.idIn;
            this.createdDateOnOrBefore = builder.createdDateOnOrBefore;
            this.createdDateOnOrAfter = builder.createdDateOnOrAfter;
            this.nameContains = builder.nameContains;
            this.isDeleteIs = builder.isDeleteIs;
        }

        public static Builder builder() {
            return new Builder();
        }

        public static class Builder {
            private Long idIs;

            private Long[] idIn;

            private LocalDateTime createdDateOnOrBefore;

            private LocalDateTime createdDateOnOrAfter;

            private String nameContains;

            private Boolean isDeleteIs;

            private Builder() {
            }

            public Filter build() {
                return Stream.of(idIs, idIn, createdDateOnOrBefore, createdDateOnOrAfter, nameContains, isDeleteIs).allMatch(Objects::isNull) ? null : new Filter(this);
            }

            public Builder idIs(Long idIs) {
                this.idIs = idIs;
                return this;
            }

            public Builder idIn(Long... idIn) {
                if (idIn != null && idIn.length == 0) {
                    throw new IllegalArgumentException("数组长度不能为 0");
                }
                this.idIn = idIn;
                return this;
            }

            public Builder createdDateOnOrBefore(LocalDateTime createdDateOnOrBefore) {
                this.createdDateOnOrBefore = createdDateOnOrBefore;
                return this;
            }

            public Builder createdDateOnOrAfter(LocalDateTime createdDateOnOrAfter) {
                this.createdDateOnOrAfter = createdDateOnOrAfter;
                return this;
            }

            public Builder nameContains(String nameContains) {
                this.nameContains = nameContains;
                return this;
            }

            public Builder isDeleteIs(Boolean isDeleteIs) {
                this.isDeleteIs = isDeleteIs;
                return this;
            }
        }
    }

    public static class Sorts extends BaseSorts {
        private Sorts(List<Sort> sorts) {
            super(sorts);
        }

        public static Builder builder() {
            return new Builder();
        }

        public static class Builder extends BaseSorts.Builder {
            public Builder id(Sort.Order order) {
                add(new Sort("id", order));
                return this;
            }

            public Sorts build() {
                return new Sorts(getSorts());
            }
        }
    }

    public static class Wraps extends BaseWraps {
        private Wraps(List<Wrap> wraps) {
            super(wraps);
        }

        public static Builder builder() {
            return new Builder();
        }

        public static class Builder extends BaseWraps.Builder {
            public Wraps build() {
                return new Wraps(getWraps());
            }
        }
    }
}
